<template>
  <div class="update-name">
    <van-nav-bar
      title="编辑昵称"
      left-text="取消"
      right-text="确定"
      @click-left="$emit('close')"
      @click-right="onClickRight"
    />

    <van-field
      v-model="message"
      rows="2"
      autosize
      border
      type="textarea"
      maxlength="15"
      placeholder="请输入新昵称"
      show-word-limit
    />
  </div>
</template>

<script>
import { updateUser } from '@/api/user'

export default {
  name: 'update-name',
  props: ['value'],
  data () {
    return {
      message: this.value
    }
  },
  methods: {
    async onClickRight () {
      try {
        if(!this.message.trim().length) {
          return this.$toast('请输入昵称')
        }

        await updateUser({
          name: this.message
        })

        this.$emit('close')
        this.$emit('input', this.message)
      } catch (err) {
        this.$toast.fail('修改昵称失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.update-name {
  padding: 0 10px;
  .van-cell {
    margin-top: 10px;
    border: 1px solid #ebedf0;
  }
}
</style>
